<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .drag {
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="drag" v-drag></div>
        <div class="drag" v-drag></div>
        <div class="drag" v-drag></div>
    </div>
    <script src="./js/vue.js"></script>
    <script>

        function drag(el) {
            // console.log(el)
            el.style.position = 'absolute'
            var startX, startY // 声明两个变量记录滑块的初始位置
            el.ontouchstart = function(e) {
                startX = e.touches[0].pageX - el.offsetLeft
                startY = e.touches[0].pageY - el.offsetTop
                el.ontouchmove = function(em) {
                    el.style.left = em.touches[0].pageX - startX + 'px'
                    el.style.top = em.touches[0].pageY - startY + 'px'
                }
            }

            el.ontouchend = function() {
                document.ontouchmove = null
            }
        }


        Vue.directive('drag', {
            bind(el) {
                drag(el)
            }
        })
        new Vue({
        }).$mount('#app')

    </script>
</body>
</html>